﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Blog.Models.MenuServices
{
    public class MenuRepository:IMenuRepository
    {
        private DataDataContext db=new DataDataContext();
        public IQueryable<Article> FindAlldata()
        {
            return db.Articles;
        }

        public IQueryable<Comment> getComment(int id)
        {
           return from x in db.Comments
            from y in db.ArticleComments
            where (y.ArticleId == id && x.CommentId == y.CommentId)
            select x;
        }

        public IQueryable<Article> FindAlldata(int id)
        {
           return from c in db.Articles
            where c.CatalogId == id
            select c;
        }

        public Article GetByIdMax()
        {
            throw new System.NotImplementedException();
        }

        public Article Getdata(int id)
        {
            return (from c in db.Articles
                    where c.ArticleId == id
                    select c).FirstOrDefault();
        }
        public int[]getNumberComment()
        {
            var c = db.Articles.ToArray();
            int[] arr = new int[c.Count()];
            for (int i = 0; i < c.Count(); i++)
            {
                arr[i] = 0;
                foreach (var x in db.ArticleComments)
                {
                    if (x.ArticleId== c[i].ArticleId)
                    {
                        arr[i] += 1;
                    }
                }
            }
            for (int i = 0; i < c.Count(); i++)
            {
                for (int j = i + 1; j < c.Count(); j++)
                {
                    if (arr[i] < arr[j])
                    {
                        var y = arr[i];
                        arr[i] = arr[j];
                        arr[j] = y;

                    }
                }
            }
            return arr;
        }
        public Article[] GetOrderComment()
        {
            var c = db.Articles.ToArray();
            int[] arr = new int[c.Count()];
            for (int i = 0; i < c.Count(); i++)
            {
                arr[i] = 0;
                foreach (var x in db.ArticleComments)
                {
                    if (x.ArticleId== c[i].ArticleId)
                    {
                        arr[i] += 1;
                    }
                }
            }

            for (int i = 0; i < c.Count(); i++)
            {
                for (int j = i + 1; j < c.Count(); j++)
                {
                    if (arr[i] < arr[j])
                    {
                        var y = c[i];
                        c[i] = c[j];
                        c[j] = y;

                    }
                }
            }
            return c;
        }

        public void Add(Article data)
        {
            db.Articles.InsertOnSubmit(data);
        }

        public void Delete(Article data)
        {
           db.Articles.DeleteOnSubmit(data);
        }
        public Article[] GetOrder()
        {
            var c = db.Articles.ToArray();
            for (int i = 0; i< c.Count(); i++)
            {
                for (int j= i+1; j < c.Count(); j++)
                {
                    if (c[i].CountReader<c[j].CountReader)
                    {
                        var y = c[i];
                        c[i]=c[j];
                        c[j] = y;
                        
                    }
                }
            }
            return c;
        }
        public void Save()
        {
           db.SubmitChanges();
        }
    }
}
